#include "newproject.h"


NewProject::NewProject(QWidget *parent)
     : QWizard(parent)
 {
    config = new Config;
     addPage(new IntroPage);
     addPage(new ClassInfoPage);
     addPage(new CulturePage);
     addPage(new BottleSpecifyPage);
     addPage(new ConclusionPage);

     ////setpixmap(QWizard::BannerPixmap, QPixmap(":/images/banner.png"));
     ////setpixmap(QWizard::BackgroundPixmap, QPixmap(":/images/background.png"));

     setWindowTitle(tr("Bimos Pro Wizard"));
 }

void NewProject::initAllArrays() {
    for (int i = 0; i < NUMBEROFBOTTLES; i++){
        activeBottles[i] = false;
        volumeBottles[i] = -1;
        volumeSludge[i] = -1;
        volumeSubstrate[i] = -1;
        if (i < 4)
            bottleGroupNames[i] = "";
    }
}

Config* NewProject::getConfig() {
    return config;
}


 void NewProject::accept()
 {
     initAllArrays();
     config = new Config;

    // check for activated bottles
     for (int i = 0; i < NUMBEROFBOTTLES; i++) {
         if (field("Bottle"+QString::number(i)).toBool()) {
             activeBottles[i] = true;
         }
     }

    // get names of cultures in the bottle
     QString bottleGroupNames[4] = {""};

     if (field("bottleName1").toBool())
         bottleGroupNames[0] = field("bottleName1").toString();
     if (field("bottleName2").toBool())
         bottleGroupNames[1] = field("bottleName2").toString();
     if (field("bottleName3").toBool())
         bottleGroupNames[2] =field("bottleName3").toString();
     if (field("bottleName4").toBool())
        bottleGroupNames[3] = field("bottleName4").toString();


    // set project name
     QString projectName = field("projectName").toString();

     for (int i = 0; i < NUMBEROFBOTTLES; i++) {
         if (activeBottles[i]) {
             QString volBottle = field("volumeBottle"+QString::number(i)).toString();
             QString volSludge = field("volumeSludge"+QString::number(i)).toString();
             QString volSubstrate = field("volumeSubstrate"+QString::number(i)).toString();
             volumeBottles[i] = volBottle.toFloat();
             volumeSludge[i] = volSludge.toFloat();
             volumeSubstrate[i] = volSubstrate.toFloat();
         }
     }

     // Apply everything to config file and write it to a file.
     config->setProjectName(projectName);
     config->setActiveBottles(activeBottles);
     config->setVolumeBottle(volumeBottles);
     config->setVolumeSludge(volumeSludge);
     config->setVolumeSubstrate(volumeSubstrate);
     config->setBottleGroupNames(bottleGroupNames);
     config->writeConfigToFile();




     QDialog::accept();
 }

 IntroPage::IntroPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Introduction"));
     setSubTitle(tr("Welcome to Bimos Pro"));
     ////setpixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark1.png"));

     label = new QLabel(tr("This wizard will generate a new project for "
                           "the Bimos box, including settings and shit. You simply "
                           "need to specify which bottles are connected to the box, "
                           "set their names and accept to produce new settings.\n\n"
                           "Please make sure that you only activate the bottles which"
                           "are really connected!"));
     label->setWordWrap(true);

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(label);
     setLayout(layout);
 }

 ClassInfoPage::ClassInfoPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Box Information"));
     setSubTitle(tr("Specify the box connectors where bottles are connected."
                    "You can also name the connectors with the cell cultures name. "
                    "This will help you to stay on top of the amount of information "
                    "and helps the Bimos pro to generate export files with the cell culture names."));
     //setpixmap(QWizard::LogoPixmap, QPixmap(":/images/logo1.png"));



    /**
      * Init box image
      */
     groupBoxActivity = new QGroupBox(tr("A&ctivity"));
     layout = new QGridLayout;
     setLayout(layout);
     *activityLabels = new QLabel[15];
     *valueLabels = new QCheckBox[15];
     *placeholderLabels = new QLabel[8];
     labelCounter = 0;
     valueLabelCounter = 0;
     placeholderCounter = 0;

     for (int i = 0; i < 15; i++) {
         activityLabels[i] = new QLabel();
         activityLabels[i]->setObjectName(QString::number(i));
         valueLabels[i] = new QCheckBox(QString::number(i+1));
         valueLabels[i]->setObjectName(QString::number(i));
         isActivated[i] = false;
     }
     for (int i = 0; i < 8; i++) {
         placeholderLabels[i] = new QLabel("|");
     }
     // init input labels for 0 - 11 with placeholders
     setupImageRow(0);
     setupValueRow(1);
     setupImageRow(2);
     setupValueRow(3);
     setupRefAndTemp();
     setupImagesForActivityBox();

     // connect it
     for (int i = 0; i < 15; i++)
         connect(valueLabels[i], SIGNAL(toggled(bool)),this,SLOT(changeColor()));

 }

 void ClassInfoPage::changeColor() {
    QObject* pObject = sender();
    QString name = pObject->objectName();
    if (!isActivated[name.toInt()]) {
        activityLabels[name.toInt()]->setPixmap(*inputActive);
        isActivated[name.toInt()] = true;
        registerField("Bottle"+QString::number(name.toInt()),valueLabels[name.toInt()]);
        qDebug() << "Bottle"+QString::number(name.toInt());
    } else {
        activityLabels[name.toInt()]->setPixmap(*inputInactive);
        isActivated[name.toInt()] = false;
    }

 }

 void ClassInfoPage::setupImagesForActivityBox() {
     loadImages();
     for (int i = 0 ; i < 15 ; i++) {
         activityLabels[i]->setScaledContents(true);
         activityLabels[i]->setPixmap(*inputInactive);
     }
 }
 void ClassInfoPage::loadImages() {
     inputActive = new QPixmap(":/img/ps2/img/ps-active-56.png");
     inputInactive = new QPixmap(":/img/ps2/img/ps-inactive-56.png");

 }

 void ClassInfoPage::setupImageRow(int gridRow) {
     for (int column = 0; column < 7; column++) {
         if (column == 3)
             layout->addWidget(placeholderLabels[placeholderCounter++],
                                       gridRow,column);
         else
             layout->addWidget(activityLabels[labelCounter++],gridRow,
                                       column);
     }
 }

 void ClassInfoPage::setupValueRow(int gridRow) {
     for (int column = 0; column < 7; column++) {
         if (column == 3)
             layout->addWidget(placeholderLabels[placeholderCounter++],
                                       gridRow,column,Qt::AlignCenter);
         else
             layout->addWidget(valueLabels[valueLabelCounter++],gridRow,
                                       column,Qt::AlignCenter);
     }
 }

 void ClassInfoPage::setupRefAndTemp() {
     // placeholders right of input boxes
     layout->addWidget(placeholderLabels[placeholderCounter++],0,7);
     layout->addWidget(placeholderLabels[placeholderCounter++],1,7);
     // image labels for reference bottles 1 and 2
     layout->addWidget(activityLabels[labelCounter++],0,8);
     layout->addWidget(activityLabels[labelCounter++],0,9);
     // image label for temperature input
     layout->addWidget(activityLabels[labelCounter++],2,9);

     layout->addWidget(valueLabels[valueLabelCounter++],1,8);
     layout->addWidget(valueLabels[valueLabelCounter++],1,9);

     layout->addWidget(valueLabels[valueLabelCounter++],3,9);
 }



 CulturePage::CulturePage(QWidget *parent)
     : QWizardPage(parent)
 {
    setTitle(tr("Culture Options"));
    setSubTitle(tr("Type in the culture names according to the connected bottles. \n\n"
                   "You can also specify a name for your project "
                 "in order to continue it after quitting or "
                 "(hopefully not) the program crashes. Your "
                 "program settings will be stored in the folder"
                 " where your program starts. \nIt will be like "
                 "2013-01-01-13-37-11-PROJECTNAME"));
    //setpixmap(QWizard::LogoPixmap, QPixmap(":/images/logo2.png"));

    projectNameLabel = new QLabel(tr("&Project name:"));
    projectNameLineEdit = new QLineEdit;
    projectNameLabel->setBuddy(projectNameLineEdit);

    bottleNameLabel1 = new QLabel(tr("&Bottle 1 - 3:"));
    bottleNameLineEdit1 = new QLineEdit;
    bottleNameLabel1->setBuddy(bottleNameLineEdit1);

    bottleNameLabel2 = new QLabel(tr("&Bottle 4 - 6:"));
    bottleNameLineEdit2 = new QLineEdit;
    bottleNameLabel2->setBuddy(bottleNameLineEdit2);

    bottleNameLabel3 = new QLabel(tr("&Bottle 7 - 9:"));
    bottleNameLineEdit3 = new QLineEdit;
    bottleNameLabel3->setBuddy(bottleNameLineEdit3);

    bottleNameLabel4 = new QLabel(tr("&Bottle 10 - 12:"));
    bottleNameLineEdit4 = new QLineEdit;
    bottleNameLabel4->setBuddy(bottleNameLineEdit4);



    QGridLayout *layout = new QGridLayout;
    layout->setColumnMinimumWidth(0, 20);
    layout->addWidget(projectNameLabel,    0, 1);
    layout->addWidget(projectNameLineEdit, 0, 2);
    layout->addWidget(bottleNameLabel1,    1, 1);
    layout->addWidget(bottleNameLineEdit1, 1, 2);
    layout->addWidget(bottleNameLabel2,    2, 1);
    layout->addWidget(bottleNameLineEdit2, 2, 2);
    layout->addWidget(bottleNameLabel3,    3, 1);
    layout->addWidget(bottleNameLineEdit3, 3, 2);
    layout->addWidget(bottleNameLabel4,    4, 1);
    layout->addWidget(bottleNameLineEdit4, 4, 2);
    setLayout(layout);
 }

void CulturePage::initializePage()
{
    registerField("projectName*",projectNameLineEdit);

    if (field("Bottle0").toBool() || field("Bottle1").toBool() || field("Bottle2").toBool()) {
        registerField("bottleName1*", bottleNameLineEdit1);
    } else {
        bottleNameLineEdit1->setEnabled(false);
    }
    if (field("Bottle3").toBool() || field("Bottle4").toBool() || field("Bottle5").toBool()){
        registerField("bottleName2*", bottleNameLineEdit2);
    } else {
        bottleNameLineEdit2->setEnabled(false);
    }
    if (field("Bottle6").toBool() || field("Bottle7").toBool() || field("Bottle8").toBool()){
        registerField("bottleName3*", bottleNameLineEdit3);
    }else {
        bottleNameLineEdit3->setEnabled(false);
    }
    if (field("Bottle9").toBool() || field("Bottle10").toBool() || field("Bottle11").toBool()){
        registerField("bottleName4*", bottleNameLineEdit4);
    }else {
        bottleNameLineEdit4->setEnabled(false);
    }
}

 BottleSpecifyPage::BottleSpecifyPage(QWidget *parent) : QWizardPage(parent)
 {
     setTitle(tr("Bottle specification"));
     setSubTitle(tr("Set the amout of bottles, sludge and substrate"));
     //setpixmap(QWizard::LogoPixmap, QPixmap(":/images/logo3.png"));
     initializeInputs();


 }

 void BottleSpecifyPage::initializePage()
 {
     for (int i = 0; i < NUMBEROFBOTTLES; i++) {
         if (!field("Bottle"+QString::number(i)).toBool()) {
             volumeLabels[i]->setEnabled(false);
             volumeSludgeEdit[i]->setEnabled(false);
             volumeSubstrateEdit[i]->setEnabled(false);
             volumeBottleEdit[i]->setEnabled(false);
        } else {
             registerField("volumeSludge"+QString::number(i)+"*", volumeSludgeEdit[i]);
             registerField("volumeSubstrate"+QString::number(i)+"*", volumeSubstrateEdit[i]);
             registerField("volumeBottle"+QString::number(i)+"*", volumeBottleEdit[i]);
         }
     }
 }

 void BottleSpecifyPage::initializeInputs()
 {
    *volumeLabels = new QLabel[NUMBEROFBOTTLES];
    *volumeSludgeEdit = new QLineEdit[NUMBEROFBOTTLES];
    *volumeSubstrateEdit = new QLineEdit[NUMBEROFBOTTLES];
    *volumeBottleEdit = new QLineEdit[NUMBEROFBOTTLES];

     QGridLayout *layout = new QGridLayout;
     layout->addWidget(new QLabel(""),0,0);
     layout->addWidget(new QLabel("Volume Bottle"),0,1);
     layout->addWidget(new QLabel("Volume Sludge"),0,2);
     layout->addWidget(new QLabel("Volume Substrate"),0,3);

     for (int i = 0; i < NUMBEROFBOTTLES; i++) {
         volumeLabels[i] = new QLabel("Bottle" + QString::number(i + 1));
         volumeSludgeEdit[i] = new QLineEdit();
         volumeSubstrateEdit[i] = new QLineEdit();
         volumeBottleEdit[i] = new QLineEdit();


         layout->addWidget(volumeLabels[i],i+1,0);
         layout->addWidget(volumeBottleEdit[i],i+1,1);
         layout->addWidget(volumeSludgeEdit[i],i+1,2);
         layout->addWidget(volumeSubstrateEdit[i],i+1,3);
         setLayout(layout);


     }
     volumeLabels[12]->setText("Bottle (Ref)" + QString::number(12));
     volumeLabels[13]->setText("Bottle (Ref)" + QString::number(13));



 }




 ConclusionPage::ConclusionPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Conclusion"));
     //setpixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark2.png"));

     label = new QLabel;
     label->setWordWrap(true);

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(label);
     setLayout(layout);
 }

 void ConclusionPage::initializePage()
 {
     QString finishText = wizard()->buttonText(QWizard::FinishButton);
     finishText.remove('&');
     label->setText(tr("Click %1 to generate the class skeleton.")
                    .arg(finishText));
 }


